package com.garlicts.framework.binlog.sync;

import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@SpringBootApplication
@ImportResource(locations = {"classpath*:applicationContext*.xml"})
@ComponentScan(basePackages = {"com.garlicts.framework.binlog.sync"})
@EnableAsync
public class GarlictsBinlogSyncApplication {

	public static void main(String[] args) {
		SpringApplication.run(GarlictsBinlogSyncApplication.class, args);
	}

	/**
	 *	异步线程池
	 */
	@Bean
	public AsyncTaskExecutor taskExecutor() {

		int availableProcessors = Runtime.getRuntime().availableProcessors();

		ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
		threadPoolTaskExecutor.setThreadNamePrefix("DbSyncExecutor-");
		threadPoolTaskExecutor.setCorePoolSize(availableProcessors * 2);
		threadPoolTaskExecutor.setMaxPoolSize(availableProcessors * 4);
		threadPoolTaskExecutor.setQueueCapacity(200);
		threadPoolTaskExecutor.setKeepAliveSeconds(60);
		// 如果队列满了，则拒绝新请求
		threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		return threadPoolTaskExecutor;
	}

}
